package com.example.guiguihomemedical.mapper;

import com.example.guiguihomemedical.entity.*;
import com.example.guiguihomemedical.entity.followup.FollowupHypertension;
import com.example.guiguihomemedical.entity.followup.Followupnewchild;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;


import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface FollowUpMapper {

    //s随访
    @Select("select *from followup where id=#{id} limit 1")
    FollowUp select(int id);
    @Insert("insert into followup(doctor_id,state,edit_time,idcard,follow_up_type,follow_up_type_id) " +
            "values(#{doctorId},#{state},now(),#{idCard},#{followUpType},#{followUpTypeId})")
    void insert(FollowUp followUp);
    @Delete("delete from followup where id=#{id}")
    void delete(FollowUp followUp);
    @Delete("delete from followup where idcard=#{idCard}")
    void deleteByIdCard(String idCard);
    @Select("select count(*) from followup where follow_up_type ='新生儿' and state =1 and idcard=#{idCard}")
    int checkNewChildeIsExist(String idCard);
    @Select("select follow_up_type from followup where idcard=#{idCard} and (state =1 or state =2) group by follow_up_type")
    List<String>getOnesFollowUpType(String idcard);

    void update(FollowUp followUp);
//s随访

    //新生儿
    @Insert("insert into followupnewchild(id_num, sex, adress, father_name, father_job, father_phone_num, father_birthday, mother_name, mother_job, mother_phone_num, mother_birthday) " +
            "values ( #{idNum}, #{sex}, #{adress}, #{fatherName}, #{fatherJob}, #{fatherPhoneNum}, #{fatherBirthday}, #{motherName}, #{motherJob}, #{motherPhoneNum}, #{motherBirthday})")
    @SelectKey(statement = "select last_insert_id()", before = false, resultType = Integer.class, keyColumn = "id", keyProperty = "id")
    int insertFollowupnewchild(Followupnewchild followupnewchild);
    @Delete("delete  from followupnewchild where id =#{id}")
    void deleteFollowupnewchild(Followupnewchild followupnewchild);
    @Select("select * from followupnewchild where id =#{id} ")
    Followupnewchild seleteFollowupnewchild(Integer id);
    void updateFollowupnewchild(Followupnewchild followupnewchild);
    //新生儿

    //高血压
    @Insert("insert into followup_hypertension(follow_up_mode, symptom, systolic_blood_pressure, diastolic_blood_pressure, heart_rate, height, weight, bmi, next_weight, next_bmi, next_other, daily_smoking_amount, next_daily_smoking_amount, daily_drink_amount, next_daily_drink_amount, medication_card_fill, precautions, next_date) " +
            "values ( #{followUpMode}, #{symptom}, #{systolicBloodPressure}, #{diastolicBloodPressure}, #{heartRate}, #{height}, #{weight}, #{bmi}, #{nextWeight}, #{nextBmi}, #{nextOther}, #{dailySmokingAmount}, #{nextDailySmokingAmount}, #{dailyDrinkAmount}, #{nextDailyDrinkAmount}, #{medicationCardFill}, #{precautions}, #{nextDate})")
    @SelectKey(statement = "select last_insert_id()", before = false, resultType = Integer.class, keyColumn = "id", keyProperty = "id")
    int insertFollowupHypertension(FollowupHypertension followupHypertension);
    @Delete("delete  from followup_hypertension where id =#{id}")
    void deleteFollowupHypertension(FollowupHypertension followupHypertension);
    @Select("select * from followup_hypertension where id =#{id} ")
    FollowupHypertension seleteFollowupHypertension(Integer id);
    void updateFollowupHypertension(FollowupHypertension hypertension);
    //高血压



    @Select("select * from followup limit #{PageBegin},#{PageSize}")
    List<FollowUp> findByPage(Integer PageBegin, Integer PageSize);

    @Select("select name,phone,residence,detailedresidence,sex from record where idcard = #{idcard}")
    @Results(id = "recordMap", value = {
            @Result(column = "sex", property = "sex", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "residence", property = "residence", jdbcType = JdbcType.VARCHAR),
            @Result(column = "detailedresidence", property = "detailedResidence", jdbcType = JdbcType.VARCHAR),
            @Result(column = "time", property = "time", jdbcType = JdbcType.VARCHAR),
    })
    Map<String, Object> findRecordByIdcard(String idcard);

    @Select("select name from doctor where id = #{id}")
    @Results(id = "doctorMap", value = {
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
    })
    Map<String, Object> findDoctorById(Integer id);



    List<FollowUp> findAndSearch(Integer PageBegin, Integer PageSize, Date dateA, Date dateB, Integer state, String type, String searchString);

}
